package com.lzy.global.exception.handler;

import com.lzy.constant.ResultCodeEnum;
import com.lzy.model.RestResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

@Api(tags = "全局异常处理", hidden = true)
@RestController
public class HxxfErrorController implements ErrorController {

    @ApiOperation(value = "全局异常处理", hidden = true)
    @GetMapping("/error")
    public RestResponseResult handleError(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        ResultCodeEnum resultCodeEnum = Optional.ofNullable(ResultCodeEnum.valueOf(statusCode)).orElse(ResultCodeEnum.服务器异常);
        return RestResponseResult.failResponse(resultCodeEnum.getCode(), resultCodeEnum.getText());
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}
